package com.rchat.pocmini.activity.groupmember;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.rchat.pocmini.activity.fragment.groups.GroupsPresenterImpl;
import com.rchat.pocmini.activity.main.MainActivity;
import com.rchat.pocmini.bean.QueryGroupMemberBean;
import com.rchat.pocmini.global.HttpCodeGlobal;
import com.rchat.pocmini.global.UrlsGlobal;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by ljd on 2017/9/7.
 */

public class GroupMemberPresenterImpl extends GroupMemberContract.Presenter {

    private ArrayList<QueryGroupMemberBean.DataBean.ChatMembersBean> data;

    @Override
    public void onPStart() {
        super.onPStart();
        data = new ArrayList<>();
    }

    @Override
    void queryGroupMember(String groupId) {
        mModel.queryGroupMember(groupId, new HttpCallbackImpl<QueryGroupMemberBean>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(QueryGroupMemberBean bean) {
                if (mView == null) {
                    return;
                }
                if (bean == null || bean.getData() == null || bean.getData().getChatMembers() == null) {
                    return;
                }
                data.clear();
                data.addAll(bean.getData().getChatMembers());
                mView.showGroupMemberListView(data);
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
            }

            @Override
            public void onFinish() {

            }

        });
    }

    @Override
    void delGroup(final String groupId) {
        new AlertDialog.Builder(mActivity).setTitle("确认删除该群组吗？")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        mModel.delGroup(groupId, AirtalkeeAccount.getInstance().getUserId(), new HttpCallbackImpl<BaseBean>() {
                            @Override
                            public void onStart() {

                            }

                            @Override
                            public void onSuccess(BaseBean baseBean) {
                                if (mView == null) {
                                    return;
                                }
                                mView.showShortToast("删除成功");
                                Intent intent = new Intent(mActivity, MainActivity.class);
                                intent.putExtra(GroupsPresenterImpl.KEY_GROUPID, groupId);
                                mActivity.startActivity(intent);
                            }

                            @Override
                            public void onFailureResult(BaseBean bean) {
                                if (mView == null) {
                                    return;
                                }
                                mView.showShortToast(bean.getDesc());
                            }

                            @Override
                            public void onFinish() {

                            }
                        });
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击“返回”后的操作,这里不设置没有任何操作
                    }
                }).show();
    }
}
